package com.yonyougov.devops.cruxcisupport.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Map;

/**
 * 使用RestTemPlate进行远程调用
 */
@Slf4j
public class HttpUtils {
    private final static RestTemplate restTemplate = new RestTemplate();

    /**
     * GET请求调用方式
     *
     * @param url          请求URL
     * @param responseType 返回对象类型
     * @return ResponseEntity 响应对象封装类
     */
    public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {
        return restTemplate.getForEntity(url, responseType);
    }

    /**
     * get請求
     *
     * @param url
     * @param header
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<T> get(String url, Map<String, String> header, Class<T> responseType) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        header.forEach((k, v) ->
                httpHeaders.add(k, v)
        );
        try {
            RequestEntity requestEntity = new RequestEntity(httpHeaders, HttpMethod.GET, new URI(url));
            return restTemplate.exchange(requestEntity, responseType);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("catch Exception when get deployment is : {}", e.getMessage());
        }
        return null;
    }

    /**
     * post請求
     *
     * @param url
     * @param header
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<T> post(String url, Map<String, String> header, Class<T> responseType) {
        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            header.forEach((k, v) ->
                    httpHeaders.add(k, v)
            );
            RequestEntity requestEntity = new RequestEntity(httpHeaders, HttpMethod.POST, new URI(url));
            return restTemplate.exchange(requestEntity, responseType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Post请求
     *
     * @param url
     * @param header
     * @param params
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<T> postWithParams(String url, Map<String, String> header, String params, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        header.forEach((k, v) ->
                httpHeaders.add(k, v)
        );
        HttpEntity<String> httpEntity = new HttpEntity<>(params, httpHeaders);
        return restTemplate.exchange(url, HttpMethod.POST, httpEntity, responseType);
    }

    public static void delete(String url, Map<String, String> header) {
        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            header.forEach((k, v) ->
                    httpHeaders.add(k, v)
            );
            RequestEntity requestEntity = new RequestEntity(httpHeaders, HttpMethod.DELETE, new URI(url));
            // 删除
            restTemplate.exchange(requestEntity, String.class);
        } catch (Exception e) {
            log.info("catch Exception when delete pod is : {}", e.getMessage());
        }
    }
}
